<template>
  <div class="container">
    <el-card shadow="never">
      <!-- 搜索 -->
      <el-form :model="requestParameters" ref="requestParameters">
        <div class="filter-container">
          <el-row>
            <el-col :span="7">
              <el-form-item label="关键字：">
                <el-input
                  style="width: 180px"
                  placeholder="根据文章标题搜索"
                  class="filter-item"
                  v-model="requestParameters.keyword"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="状态：">
                <el-select
                  class="filter-item"
                  style="width: 220px"
                  v-model="requestParameters.state"
                >
                  <el-option
                    v-for="item in status"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item>
                <el-button size="small" @click="resetForm">清除</el-button>
                <el-button size="small" type="primary" @click="searchArticle"
                  >搜索</el-button
                >
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-button
                class="filter-item fr"
                size="small"
                style="margin-left: 10px"
                @click="addArticle"
                type="success"
                icon="el-icon-edit"
                >新增技巧</el-button
              >
            </el-col>
          </el-row>
        </div>
      </el-form>
      <!-- 数据记录 -->
      <el-alert
        :title="alertText"
        type="info"
        class="alert"
        show-icon
      ></el-alert>
      <!-- end -->
      <!-- 数据列表 -->
      <el-table
        :data="dataList"
        fit
        highlight-current-row
        style="width: 100%"
        :header-cell-style="{'border-bottom': '2px solid #e8e8e8',fontSize:'15px'}"
      >
        <el-table-column align="center" label="序号" type="index" width="70">
        </el-table-column>
        <el-table-column align="center" label="文章标题">
          <template slot-scope="scope">
            <span>{{ scope.row.title }}</span>
            <i
              class="el-icon-film"
              style="color:blue;font-size:20px"
              v-if="scope.row.videoURL"
              @click="videoPlay(scope.row.videoURL)"
            ></i>
          </template>
        </el-table-column>
        <el-table-column align="center" label="阅读数">
          <template slot-scope="scope">
            <span>{{ scope.row.visits }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="录入人">
          <template slot-scope="scope">
            <span>{{ scope.row.username }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="录入时间">
          <template slot-scope="scope">
            <span>{{ formateDate(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.state == 1">已启用</span>
            <span v-else>已禁用</span>
          </template>
        </el-table-column>
        <!-- 操作按钮 -->
        <el-table-column
          align="center"
          label="操作"
          width="300"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="perview(scope.row.id)"
              >预览</el-button
            >
            <el-button type="text" size="mini" @click="tranState(scope.row)">
              {{ scope.row.state === 1 ? "禁用" : "启用" }}
            </el-button>
            <el-button
              :disabled="scope.row.state === 0"
              type="text"
              size="mini"
              @click="edit(scope.row.id)"
              >修改</el-button
            >
            <el-button
              :disabled="scope.row.state === 0"
              size="mini"
              type="text"
              @click="deleteUser(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- end -->

      <!-- 分页 -->
      <div class="pagination" style="float:right; margin:20px 0 20px 0">
        <div class="pages">
          <el-pagination
            background
            :hide-on-single-page="value"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="Number(requestParameters.page)"
            :total="Number(total)"
            :page-size="Number(requestParameters.pagesize)"
            :page-sizes="[5, 10, 15, 20]"
            layout="sizes, prev, pager, next, jumper"
          ></el-pagination>
        </div>
      </div>
      <!-- 新增标签弹层 -->
      <articleAdd
        :form="formData"
        :showDialog="showDialog"
        @newData="getList"
        @closeDialog="closeDialog"
      ></articleAdd>

      <!-- 预览标签弹层 -->
      <articlePre :form="formData" @noShow="noShow" :show="show"></articlePre>

      <!-- 外层的遮罩 v-if用来控制显示隐藏 点击事件用来关闭弹窗 -->
      <div class="mask" v-if="videoState == true"></div>
      <!-- 弹窗 -->
      <div class="videomasks" v-if="videoState == true">
        <!-- 视频：h5的视频播放video -->
        <i  class="el-icon-circle-close" @click="masksCloseFun"></i>
        <video :src="videoSrc" controls="controls" autoplay></video>
      </div>
      <!-- end -->
    </el-card>
  </div>
</template>

<script>
import { list, remove, detail, changeState } from '@/api/hmmm/articles.js'
import { status } from '@/api/hmmm/constants'
import articleAdd from '../components/articles-add.vue'
import articlePre from '../components/articles-preview.vue'
import dayjs from 'dayjs'
export default {
  name: 'Articles',
  components: {
    articleAdd,
    articlePre
  },
  data () {
    return {
      value: false,
      // 数据列表
      dataList: [],
      // 数据总数
      total: null,
      // 显示文本
      alertText: '',
      // 数据列表的请求参数
      requestParameters: {
        page: 1,
        pagesize: 5,
        keyword: null,
        state: null
      },
      disabled: false,
      // 视频地址
      videoSrc: '',
      // 控制视频播放
      videoState: false,
      // 添加和编辑框的显示
      showDialog: false,
      // 添加和编辑框的表单数据
      formData: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      // 预览框的显示
      show: false,
      curId: null
    }
  },
  computed: {
    status () {
      return status
    }
  },
  mounted () {
    // 初始化数据
    this.initDate()
  },
  methods: {
    // 初始数据
    initDate () {
      // 读取数据
      this.getList()
    },
    // 获取列表数据
    async getList () {
      const { data: res } = await list(this.requestParameters)
      // 获取数据给dataList
      console.log(res)
      this.dataList = res.items
      // 获取总条数
      this.total = res.counts
      this.alertText = `共 ${this.total} 条记录`
    },
    // 视频播放
    videoPlay (video) {
      this.videoState = true
      this.videoSrc = video
    },
    // 视频关闭
    masksCloseFun () {
      this.videoState = false
    },
    // 时间处理函数
    formateDate (value, str = 'YYYY-MM-DD HH:MM:ss') {
      return dayjs(value).format(str)
    },
    // 禁用启用按钮
    tranState (val) {
      this.$confirm('是否修改操作?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          if (val.state === 1) {
            val.state = 0
          } else {
            val.state = 1
          }
          const data = {
            id: val.id,
            state: val.state
          }
          await changeState(data)
            .then((response) => {
              this.$message.success('已成功')
            })
            .catch((response) => {
              this.$message.error('失败!')
            })
        })
        .catch(() => {
          this.$message.info('已取消操作!')
        })
    },
    // 删除功能
    deleteUser (id) {
      this.$confirm('此操作将永久删除用户, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          // 调用删除接口
          await remove({ id }).then(() => {
            this.getList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 获取文章详情 数据回显
    async hGetArticleDetail (id) {
      const res = await detail({ id })
      this.formData = res.data
    },
    // 修改功能
    edit (id) {
      // 打开弹框
      this.showDialog = true
      // 数据回显
      this.hGetArticleDetail(id)
    },
    // 添加功能
    addArticle () {
      this.showDialog = true
      this.formData = {}
    },
    // 预览功能
    perview (id) {
      console.log(id)
      this.show = true
      this.hGetArticleDetail(id)
    },
    // 关闭添加和编辑弹窗
    closeDialog () {
      this.showDialog = false
    },
    // 关闭预览弹窗
    noShow () {
      this.show = false
    },

    // 重置清除
    async resetForm () {
      this.requestParameters.keyword = null
      this.requestParameters.state = null
    },
    // 搜索信息
    searchArticle () {
      this.requestParameters.page = 1
      this.getList(this.requestParameters)
      this.value = true
    },
    // 每页显示信息条数
    handleSizeChange (val) {
      this.requestParameters.pagesize = val
      this.getList(this.requestParameters)
    },
    // 进入某一页
    handleCurrentChange (val) {
      this.requestParameters.page = val
      this.getList()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-form-item--medium {
    margin-bottom: 0px;
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
}
.el-table th {
  background-color: #fafafa;
}
.el-table th.is-leaf {
  border-bottom: 2px solid #e8e8e8;
}
.disabled td {
  background-color: #f9f9f9;
  color: #c1c1c1;
}
.disabled .el-button--primary,
.disabled .el-button--danger {
  background-color: #dbdada;
  border: 1px solid #dbdada;
  color: #999;
  cursor: not-allowed;
}
.mask {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 10;
  background-color: #000000;
  opacity: 0.6;
}

// 内容层 z-index要比遮罩大，否则会被遮盖
.videomasks {
  max-width: 800px;
  position: fixed;
  left: 50%;
  top: 50%;
  z-index: 20;
  transform: translate(-50%, -50%);
  i {
    position: absolute;
    font-size: 50px;
    left: 50%;
    top: -50px;
    background-color: transparent;
  }
}
.videomasks video {
  width: 100%;
  height: 100%;
}
</style>
